<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.generate.image.php
 * Type:     function
 * Name:     assign
 * Purpose:  search image follow width and height, if not exists then generate
 * Author:   Pham Van Diep
 * Date:     13/2/2014
 * Params:   string path : path to directory 
 *			 string name : name of image file
 *			 int width   : image width
 *			 int height  : image height
 *			 string png  : file type not include dot '.'
 * Return:   string image path else return empty
 * -------------------------------------------------------------
 */
function smarty_function_generate_image($params , &$template) {			
	$strPath   = isset($params['path'])   ? rtrim($params['path'],'/') . '/' : '' ;
	$strName   = isset($params['name'])   ? rtrim($params['name'], '.')      : '';
    //$strName   = str_replace(array('  '),'', $strName);
	$intWidth  = isset($params['width'])  ? intval($params['width'])         : IMAGE_LOGO_SIZE;
	$intHeight = isset($params['height']) ? intval($params['height'])        : IMAGE_LOGO_SIZE;
	$strAlt    = isset($params['alt'])    ? trim($params['alt'])             : HOST_DOMAIN;
	$strPng     = isset($params['ext'])   ? '.' . ltrim($params['ext'] , '.'): '';
    
	if(empty($strPath) || empty($strName) || empty($strPng)) return '';// DEFAULT IMAGE ERROR 	
	$strImagePath = $strPath.$strName.$intWidth.'x'.$intHeight. $strPng	;
	// if exists	
	if( file_exists (DATA_UPLOAD_DIR.$strImagePath ) ) {
		return '<img src="'.CI_Function::getImageUrl($strPath,$strName, $intWidth, $intHeight,$strPng).'" alt="'.$strAlt.'" />';
	}
	$strImagePath = $strPath.$strName. $strPng;
	if(! file_exists(DATA_UPLOAD_DIR.$strImagePath) ) return '';//DEFAULT IMAGE ERROR
	else {
		CI_Function::generate($strImagePath, $intWidth, $intHeight );
		return '<img src="'.CI_Function::getImageUrl($strPath,$strName, $intWidth, $intHeight,$strPng).'" alt="'.$strAlt.'" />';
	}    
}